package com.jt.service;

import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;

@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private ItemCatMapper itemCatMapper;

    /**
     * 把数据库的所有数据取出,装到map集合
     * map<parent_id,list<ItemCat>>
     */
    public Map<Integer,List<ItemCat>> getMap(){
        Map<Integer,List<ItemCat>> map = new HashMap<>();
        //1.查询所有的数据库信息
        List<ItemCat> list = itemCatMapper.findItemCatAll();
        //2.遍历数据封装Map
        for(ItemCat itemCat : list){
            int key = itemCat.getParentId();
            if(map.containsKey(key)){
                map.get(key).add(itemCat);
            }else{
                List<ItemCat> tempList = new ArrayList<>();
                tempList.add(itemCat);
                map.put(key,tempList);
            }
        }
        return map;
    }

    /**
     * 获取二级菜单的方法
     * @param map
     * @return
     */
    private List<ItemCat> getTwoList(Map<Integer, List<ItemCat>> map) {
        //1.查询一级菜单的数据
        List<ItemCat> oneList = map.get(0);
        //遍历一级菜单
        for(ItemCat oneItemCat : oneList){
            //获取一级菜单的ID
            int id = oneItemCat.getId();
            //根据一级菜单的ID,查询二级数据
            List<ItemCat> twoList = map.get(id);
            oneItemCat.setChildren(twoList);
        }
        return oneList;
    }



    /**
     * 获取三级菜单的方法
     * @param map
     * @return
     */
    private List<ItemCat> getThreeList(Map<Integer, List<ItemCat>> map) {
        List<ItemCat> oneList = getTwoList(map);
        for(ItemCat oneItemCat:oneList){
            List<ItemCat> twoList = oneItemCat.getChildren();
            if(twoList == null || twoList.size()==0){
                continue;
            }
            for(ItemCat twoItemCat :twoList){
                Integer id = twoItemCat.getId();
                List<ItemCat> threeList = map.get(id);
                twoItemCat.setChildren(threeList);
            }
        }
        return oneList;
    }



    /*
    @Override
    public List<ItemCat> findItemCatList(Integer level) {
        //方式一:获取列表三级菜单(双重for循环!!!)多次查数据库,效率低
        //1.查询一级商品分类信息
        int parentId = 0;
        List<ItemCat> oneList = itemCatMapper.findItemCatList(parentId);
        //2.查询二级商品分类信息
        for (ItemCat oneItemCat : oneList) {
            int oneId = oneItemCat.getId();
            List<ItemCat> twoList = itemCatMapper.findItemCatList(oneId);
            for (ItemCat twoItemCat : twoList) {
                int twoId = twoItemCat.getId();
                List<ItemCat> threeList = itemCatMapper.findItemCatList(twoId);
                twoItemCat.setChildren(threeList);
            }
            oneItemCat.setChildren(twoList);
        }
        return oneList;
    }
     */


    /**
     * 查询三级菜单返回前端处理
     * 7.1 查询3级商品分类信息
     * @param level
     * @return
     */
    @Override
    public List<ItemCat> findItemCatList(Integer level) {
        //业务层三级菜单的优化:方式二,Map实现
        //获取数据库的所有数据,用map封装map<parent_id,list<ItemCat>>
        Map<Integer, List<ItemCat>> map = getMap();
        //level=1,只查询一级数据
        if(level==1){
            return map.get(0);
        }
       //level=2,查询一级和二级数据
        if(level==2){
            return getTwoList(map);
        }
        //level=3,查询一级和二级和三级数据
        List<ItemCat> threeList = getThreeList(map);
        return threeList;
    }


    /**
     * 7.3商品分类新增
     * @param itemCat
     */
    @Override
    @Transactional //事务控制
    public void saveItemCat(ItemCat itemCat) {
        Date date = new Date();
        itemCat.setStatus(true).setCreated(date).setUpdated(date);
        itemCatMapper.saveItemCat(itemCat);
    }

    @Override
    @Transactional
    public void updateStatus(ItemCat itemCat) {
        itemCat.setUpdated(new Date());
        itemCatMapper.updateStatus(itemCat);
    }

    @Override
    @Transactional
    public void updateItemCat(ItemCat itemCat) {
        itemCat.setUpdated(new Date());
        itemCatMapper.updateItemCat(itemCat);
    }

    @Override
    @Transactional
    public void deleteItemCat(ItemCat itemCat) {
        //判断level到底是几级菜单
        if(itemCat.getLevel() == 3){
            //三级菜单,直接删除
            itemCatMapper.deleteItemCatById(itemCat.getId());
        }
        if(itemCat.getLevel() == 2){
            //二级菜单,删除三级菜单和二级菜单
            itemCatMapper.deleteItemCat2(itemCat.getId());
        }
        if(itemCat.getLevel() == 1){
            //三级菜单,删除三级菜单,二级菜单和一级菜单
            itemCatMapper.deleteItemCat1(itemCat);
        }
    }
























}
